package com.liannong.controller;

import com.liannong.common.Result;
import com.liannong.entity.TransportDetails;
import com.liannong.service.TransportDetailsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/transport")
public class TransportDetailsController {
    
    @Autowired
    private TransportDetailsService transportDetailsService;
    
    /**
     * 创建运输记录
     */
    @PostMapping("/save")
    public Result createTransport(@RequestBody TransportDetails transportDetails) {
        boolean result = transportDetailsService.createTransport(
            transportDetails.getOrderId(), 
            transportDetails.getVehicleId(), 
            transportDetails.getStartLocation(), 
            transportDetails.getEndLocation(), 
            transportDetails.getCurrentTemp()
        );
        return result ? Result.success() : Result.error();
    }
    
    /**
     * 更新运输温度
     */
    @PutMapping("/updateTemperature/{transportId}")
    public Result updateTemperature(@PathVariable Long transportId, @RequestBody TransportDetails transportDetails) {
        boolean result = transportDetailsService.updateTemperature(transportId, transportDetails.getCurrentTemp());
        return result ? Result.success() : Result.error();
    }
    
    /**
     * 更新运输状态
     */
    @PutMapping("/updateStatus/{transportId}")
    public Result updateStatus(@PathVariable Long transportId, @RequestBody TransportDetails transportDetails) {
        boolean result = transportDetailsService.updateStatus(transportId, transportDetails.getStatus());
        return result ? Result.success() : Result.error();
    }
    
    /**
     * 获取订单的运输详情
     */
    @GetMapping("/order/{orderId}")
    public Result getOrderTransport(@PathVariable String orderId) {
        TransportDetails transportDetails = transportDetailsService.getOrderTransport(orderId);
        return Result.success(transportDetails);
    }
    
    /**
     * 获取车辆的所有运输记录
     */
    @GetMapping("/vehicle/{vehicleId}")
    public Result getVehicleTransports(@PathVariable String vehicleId) {
        List<TransportDetails> transportDetailsList = transportDetailsService.getVehicleTransports(vehicleId);
        return Result.success(transportDetailsList);
    }
    
    /**
     * 获取所有运输记录
     */
    @GetMapping("/findAll")
    public Result getAllTransports() {
        List<TransportDetails> transportDetailsList = transportDetailsService.getAllTransports();
        return Result.success(transportDetailsList);
    }
    
    /**
     * 检查订单相关产品的温度情况
     */
    @GetMapping("/checkTemperature/{orderId}")
    public Result checkOrderProductsTemperature(@PathVariable String orderId) {
        Map<Long, Boolean> checkResult = transportDetailsService.checkOrderProductsTemperature(orderId);
        
        // 检查是否有温度异常的产品
        boolean hasTemperatureIssue = checkResult.containsValue(false);
        
        // 创建返回结果对象
        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("status", hasTemperatureIssue ? "异常" : "正常");
        resultMap.put("message", hasTemperatureIssue ? "温度异常，请注意查看详情" : "温度正常");
        resultMap.put("details", checkResult);
        
        return Result.success(resultMap);
    }
} 